﻿@{
    ViewBag.Title = "Index";
    Layout = "../Shared/_Layout.cshtml";
}

@using mscook.mvc;
@model mscook.mvc.Areas.weblogin.Models.ArticleIndexModel
@Html.Partial("_DeleteModal")
<div id="alertModal" class="modal hide fade form-horizontal" title="提示框" aria-hidden="false" style="display: none;">
    <div class="modal-header">
        <h4>提示框</h4>
    </div>

    <div class="modal-body">
        <div>
            发布成功！
        </div>
    </div>
    <div class="modal-footer">
        <a href="#" class="btn btn-primary" data-dismiss="modal">关闭</a>
    </div>

</div>
<div>
    <ul class="breadcrumb">
        <li>
            <a href="#">首页</a> <span class="divider">/</span>
        </li>
        <li>
            <a href="#">杂志</a><span class="divider">/</span>
        </li>
        <li>
            <a href="#">文章</a>
        </li>
    </ul>
</div>
<div class="row-fluid sortable">
    <div class="box span12">
        <div class="box-header well" data-original-title="杂志列表">
            <h2>
                <i class="icon-list-alt"></i>
                文章</h2>
            <div class="box-icon">
                <a href="@Url.Action("GenTemplate" + "/" + ViewData["Year"] + "/" + ViewData["Month"])"  class="btn btn-setting btn-round"><i class="icon-plus-sign"></i></a>
            </div>
        </div>
        <div class="box-content">
            <table class="table table-striped table-bordered bootstrap-datatable ">
                <thead>
                    <tr>
                        <th>年份</th>
                        <th>期数</th>
                        <th>索引</th>
                        <th>主题</th>
                        <th>所需积分</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var Article in Model.ArticlePublishDetails)
                    {
                        var needBonus = Math.Abs(Model.ResourceBonusSettings.Where(o => o.ResourceID.Equals(Article.ArticleKey.Id())).Select(o => o.Bonus).SingleOrDefault());
                        <tr>
                            <td>@Html.Encode(Article.ArticleKey.Year)</td>
                            <td class="center">@Html.Encode(Article.ArticleKey.Month)</td>
                            <td class="center">@Html.Encode(Article.ArticleKey.Index)</td>
                            <td class="center">@Html.Encode(Article.DataOfUnit.title)</td>
                                 <td class="center"><span class="bonus" contentEditable="true" style="width:100%;display:block;padding:0 0 10px 0;" data-oldVal="@needBonus" data-url="@Url.Action("SetBonus", "home", new {resourceId=Article.ArticleKey.Id(),purchasedCategory=mscook.core.domain.PurchasedCategory.Article })">@Html.Encode(needBonus)</span></td>
                              <td class="center status">@Html.Encode(Article.isPublished?"已发布":"未发布")</td>
                            <td class="center" data-id="@Article.DataOfUnit.title">
                                <a class="btn btn-info" href="@Url.Action("GenTemplate", new { year = Article.ArticleKey.Year, month = Article.ArticleKey.Month, alias = Article.ArticleKey.Alias })">
                                    <i class="icon-edit icon-white"></i>
                                    修改                                            
                                </a>
                                <a class="btn btn-success"  data-url="@Url.Action("publish", new { year = Article.ArticleKey.Year, month = Article.ArticleKey.Month, alias = Article.ArticleKey.Alias, index = Article.ArticleKey.Index })" data-target="#">
                                    <i class="icon-edit icon-white"></i>
                                    发布                                            
                                </a>

                                 <a class="btn btn-danger" href="@Url.Action("ArticleDelete", new { year=Article.ArticleKey.Year,month=Article.ArticleKey.Month,alias=Article.ArticleKey.Alias,index=Article.ArticleKey.Index })">
                                    <i class="icon-trash icon-white"></i>
                                    删除
                                </a>

                            </td>
                        </tr>
                    }

                </tbody>
            </table>
        </div>
    </div>
</div>
@section JavaScript
{
    <script type="text/javascript">
        $(function () {
            $("a.btn-success").click(function () {
                var url = $(this).data("url");
                var $status = $(this).parent().parent().find("td.status")
                $.ajax({
                    url: url,
                    type: "post",
                    success: function () {
                        $status.html("已发布");
                        $("#alertModal").modal("show");
                    }
                })
            });

            $("span.bonus").blur(function () {
                var $this=$(this);
                var url = $this.data("url");
                var oldVal =$this.data("oldval");
                var bonus = $this.html();
                bonus = parseInt(bonus);
                if (!isNaN(bonus) && oldVal!=bonus.toString()) {
                    $.getJSON(url, { bonus: bonus }, function () {
                        $this.attr("data-oldval", bonus);
                    });
                }
 
            });
        });

    </script>
}